package cn.hl.controller;

import cn.hl.dto.MovieTicketDto;
import cn.hl.result.JSONResult;
import cn.hl.sentinel.MovieFallBack;
import cn.hl.service.IMovieTicketService;
import cn.hl.domain.MovieTicket;
import cn.hl.query.MovieTicketQuery;
import cn.hl.result.PageList;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @SentinelResource(value = "movie-server",fallback = "saveOrUpdate",fallbackClass = MovieFallBack.class)
    public JSONResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    @SentinelResource(value = "movie-server",fallback = "delete",fallbackClass = MovieFallBack.class)
    public JSONResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JSONResult.success();
    }

    /**
    * 获取对象
    */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @SentinelResource(value = "movie-server",fallback = "get",fallbackClass = MovieFallBack.class)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(movieTicketService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @SentinelResource(value = "movie-server",fallback = "list",fallbackClass = MovieFallBack.class)
    public JSONResult list(){
        return JSONResult.success(movieTicketService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    @SentinelResource(value = "movie-server",fallback = "page",fallbackClass = MovieFallBack.class)
    public JSONResult page(@RequestBody MovieTicketQuery query){
        Page<MovieTicket> page = new Page<MovieTicket>(query.getPage(),query.getRows());
        page = movieTicketService.selectPage(page);
        return JSONResult.success(new PageList<MovieTicket>(page.getTotal(),page.getRecords()));
    }

    /**
     * 根据Id进行购买
     */
    @RequestMapping(value="/buyMovieTicketById",method= RequestMethod.POST)
    @SentinelResource(value = "movie-server",fallback = "buyMovieTicketById",fallbackClass = MovieFallBack.class)
    public JSONResult buyMovieTicketById(@RequestBody MovieTicketDto movieTicketDto) {
        return movieTicketService.buyMovieTicketById(movieTicketDto);
    }

    /**
     * 根据Id查询用户的电影票信息
     */
    @RequestMapping(value="/findMovieTicketByUserId/{id}",method= RequestMethod.POST)
    @SentinelResource(value = "movie-server",fallback = "buyMovieTicketById",fallbackClass = MovieFallBack.class)
    public JSONResult findMovieTicketByUserId(@PathVariable("id")Long id) {
        return JSONResult.success(movieTicketService.findMovieTicketByUserId(id));
    }

    /**
     * 根据电影Id观看电影
     */
    @RequestMapping(value="/watchMovieById",method= RequestMethod.POST)
    @SentinelResource(value = "movie-server",fallback = "watchMovieById",fallbackClass = MovieFallBack.class)
    public JSONResult watchMovieById(@RequestBody MovieTicketDto movieTicketDto) throws Exception {
        return movieTicketService.watchMovieById(movieTicketDto);
    }

    /**
     * 随机赠送电影票
     */
    @RequestMapping(value="/sendMovieTicket/{id}",method= RequestMethod.POST)
    @SentinelResource(value = "movie-server",fallback = "sendMovieTicket",fallbackClass = MovieFallBack.class)
    public JSONResult sendMovieTicket(@PathVariable("id")Long id) {
        movieTicketService.sendMovieTicket(id);
        return JSONResult.success();
    }
}
